package com.szp.drug.component.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szp.drug.component.mapper.DrugInstrumentMapper;
import com.szp.drug.component.service.api.DrugInstrumentService;
import com.szp.drug.entity.DrugInstrument;
import com.szp.drug.util.ResultEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 器械实现类
 * @author SuZePing
 * @create 2021-03-06 10:04
 * @E-mail suzeping10@126.com
 */
@Slf4j
@Service
public class DrugInstrumentServiceImpl extends ServiceImpl<DrugInstrumentMapper, DrugInstrument> implements DrugInstrumentService {

	@Autowired
	private DrugInstrumentMapper instrumentMapper;

	/**
	 * 分页获取器械
	 * @param instrumentName
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResultEntity<PageInfo<DrugInstrument>> getPageInfo(String instrumentName, String pageNum, String pageSize) {
		// 开启分页
		PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
		QueryWrapper<DrugInstrument> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted","0");
		wrapper.and(m -> m.like("instrument_name",instrumentName))
				.orderByAsc("create_time");
		List<DrugInstrument> instruments = instrumentMapper.selectList(wrapper);
		log.debug("返回 instruments:{}",instruments);
		return ResultEntity.success(new PageInfo<>(instruments));
	}

	/**
	 * 新增器械
	 * @param instrument
	 * @return
	 */
	@Override
	public ResultEntity<Integer> saveInstrument(DrugInstrument instrument) {
		QueryWrapper<DrugInstrument> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted","0").eq("instrument_name",instrument.getInstrumentName());
		List<DrugInstrument> selInstruments = instrumentMapper.selectList(wrapper);
		if (selInstruments.size() != 0){
			return ResultEntity.error("器械名重复！");
		}
		instrument.setInstrumentId("IN" + IdUtil.fastSimpleUUID().substring(0, 10))
				.setCreateTime(DateUtil.now());
		instrumentMapper.insert(instrument);
		// 返回新增后总数量 为数据展示页码使用
		QueryWrapper<DrugInstrument> instrumentWrapper = new QueryWrapper<>();
		instrumentWrapper.eq("deleted","0");
		Integer count = instrumentMapper.selectCount(instrumentWrapper);
		return ResultEntity.success(count);
	}

	/**
	 * 修改器械
	 * @param instrument
	 * @return
	 */
	@Override
	public ResultEntity<String> editInstrument(DrugInstrument instrument) {
		String instrumentId = instrument.getInstrumentId();
		if (StrUtil.hasEmpty(instrumentId)){
			return ResultEntity.error("主键id不能为空!");
		}
		String instrumentName = instrument.getInstrumentName();
		QueryWrapper<DrugInstrument> wrapper = new QueryWrapper<>();
		wrapper.eq("deleted","0").eq("instrument_name",instrumentName);
		List<DrugInstrument> selInstruments = instrumentMapper.selectList(wrapper);
		if (selInstruments.size() != 0 && !Objects.equals(instrumentId,selInstruments.get(0).getInstrumentId())){
			return ResultEntity.error("器械名重复！");
		}
		instrumentMapper.updateById(instrument);
		return ResultEntity.success();
	}

	/**
	 * 删除器械
	 * @param instrumentId
	 * @return
	 */
	@Override
	public ResultEntity<String> removeInstrument(String instrumentId) {
		if (StrUtil.hasEmpty(instrumentId)){
			return ResultEntity.error("主键id不能为空!");
		}
		DrugInstrument instrument = new DrugInstrument();
		instrument.setInstrumentId(instrumentId).setDeleted("1");
		instrumentMapper.updateById(instrument);
		return ResultEntity.success();
	}
}
